package com.sany.canwifi.udp;

import android.content.Context;
import android.util.Log;

import com.sany.canwifi.util.NetWorkUtils;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * author : dingqb
 * e-mail : dingqb@sany.com.cn
 * date   : 2022/1/4 5:45 PM
 * desc   :
 * version: 1.0
 */
public class Send {
    public static final String TAG = "Send";

    public static void send(Context context) throws Exception {
        InetAddress addr = InetAddress.getLocalHost();
        String hostname = addr.getHostAddress();
        System.out.println(hostname);
        Log.e(TAG, "send: hostname==" + hostname);
        DatagramSocket socket = new DatagramSocket();
        byte[] buf = "android client ".getBytes();
        String ip = "192.168.3.170";
        Log.e(TAG, "send: ip=="+ip );
        //将数据打包
        DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName(ip), 8080);
        for (int i = 0; i < 5; i++) {
            Log.e(TAG, "send: " + i);
            socket.send(packet);
            Thread.sleep(200);
        }
        socket.close();

    }
}
